/**
 * 研发服务
 */
const BaseProjectService = require('./base_project_service.js');
const util = require('../../../framework/helpers/util.js');
const cloudUtil = require('../../../framework/cloud/cloud_util.js');
const cloudBase = require('../../../framework/cloud/cloud_base.js');
const ResearchProjectService = require('./research_project_service.js');
// 使用统一配置入口引用模型
const { ProjectFeedbackModel } = require('../config/models_config.js');

class ResearchService extends BaseProjectService {

    constructor() {
        super();
        this._projectService = null;
        this._timestamp = this._getTimestamp();
    }

    /**
     * 获取研发项目父项目 - 桥接方法
     * @param {String} id 项目ID
     */
    async getResearchParent(id) {
        try {
            // 确保项目服务已初始化
            if (!this._projectService) {
                this._projectService = new ResearchProjectService();
            }

            // 调用项目服务的方法获取父项目
            const project = await this._projectService.getProjectDetail(id);
            if (!project || !project.PARENT_ID) {
                console.log('[getResearchParent] 项目不存在或没有父项目, ID:', id);
                return null;
            }

            // 获取父项目详情
            const parentProject = await this._projectService.getProjectDetail(project.PARENT_ID);
            if (!parentProject) {
                console.log('[getResearchParent] 父项目不存在, ID:', project.PARENT_ID);
                return null;
            }

            return parentProject;
        } catch (err) {
            console.error('[getResearchParent] 获取研发项目父项目失败:', err);
            return null;
        }
    }

    /**
     * 获取研发项目分支列表 - 桥接方法
     * @param {String} parentId 父项目ID
     * @param {Number} page 页码
     * @param {Number} size 每页数量
     */
    async getResearchBranchList(parentId, page = 1, size = 20) {
        try {
            // 确保项目服务已初始化
            if (!this._projectService) {
                this._projectService = new ResearchProjectService();
            }

            // 调用项目服务的方法获取分支列表
            return await this._projectService.getProjectBranchList(parentId, page, size);
        } catch (err) {
            console.error('[getResearchBranchList] 获取研发项目分支列表失败:', err);
            return {
                list: [],
                total: 0
            };
        }
    }

    /**
     * 添加项目反馈
     * @param {Object} data 反馈数据
     * @returns {Promise<Object>} 操作结果
     */
    async addProjectFeedback(data) {
        const model = new ProjectFeedbackModel();
        
        data.FEEDBACK_ADD_TIME = this._timestamp;
        data.FEEDBACK_EDIT_TIME = this._timestamp;
        
        const id = await model.insert(data);
        return { id };
    }

    /**
     * 更新项目反馈
     * @param {String} feedbackId 反馈ID
     * @param {Object} data 反馈数据
     * @returns {Promise<Boolean>} 操作结果
     */
    async updateProjectFeedback(feedbackId, data) {
        const model = new ProjectFeedbackModel();
        
        data.FEEDBACK_EDIT_TIME = this._timestamp;
        
        await model.edit({ FEEDBACK_ID: feedbackId }, data);
        return true;
    }

    /**
     * 获取项目反馈详情
     * @param {String} feedbackId 反馈ID
     * @returns {Promise<Object>} 反馈详情
     */
    async getProjectFeedbackDetail(feedbackId) {
        const model = new ProjectFeedbackModel();
        
        const feedback = await model.getOne({ FEEDBACK_ID: feedbackId });
        if (!feedback) {
            throw new Error('反馈不存在');
        }
        
        return feedback;
    }

    /**
     * 获取项目反馈列表
     * @param {Object} where 查询条件
     * @param {Object} options 查询选项
     * @returns {Promise<Array>} 反馈列表
     */
    async getProjectFeedbackList(where, options = {}) {
        const model = new ProjectFeedbackModel();
        
        const list = await model.getAll(where, options);
        return list;
    }

    /**
     * 删除项目反馈
     * @param {String} feedbackId 反馈ID
     * @returns {Promise<Boolean>} 操作结果
     */
    async deleteProjectFeedback(feedbackId) {
        const model = new ProjectFeedbackModel();
        
        await model.del({ FEEDBACK_ID: feedbackId });
        return true;
    }

    /**
     * 添加项目反馈评论
     * @param {String} feedbackId 反馈ID
     * @param {Object} comment 评论数据
     * @returns {Promise<Boolean>} 操作结果
     */
    async addProjectFeedbackComment(feedbackId, comment) {
        const model = new ProjectFeedbackModel();
        
        const feedback = await model.getOne({ FEEDBACK_ID: feedbackId });
        if (!feedback) {
            throw new Error('反馈不存在');
        }
        
        const comments = feedback.FEEDBACK_COMMENTS || [];
        comments.push({
            ...comment,
            time: this._timestamp
        });
        
        await model.edit({ FEEDBACK_ID: feedbackId }, {
            FEEDBACK_COMMENTS: comments,
            FEEDBACK_EDIT_TIME: this._timestamp
        });
        
        return true;
    }
}

module.exports = ResearchService; 